import model.ListNode;

public class SwapPairs {
    //两两交换链表中的节点
    public ListNode code(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode newHead = new ListNode(0);
        newHead.next = head;

        ListNode prev = newHead, cur = prev.next, next = cur.next, nnext = next.next;
        //执行交换操作
        while (cur != null && next != null) {
            prev.next = next;
            cur.next = nnext;
            next.next = cur;

            //修改
            prev = cur;
            cur = nnext;
            if (cur != null) {
                next = cur.next;
            }

            if (next != null) {
                nnext = next.next;
            }
        }

        return newHead.next;

    }
}
